﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace CPPEI.Coolzon.UI.DataPartLib
{
    public partial class DataTextAnimationLabel
    {
        private string _XText;
        [Category("其他")]
        [DisplayName("显示文本")]
        [Description("XText")]
        [Editor(typeof(UI.XProperty.Editors.XStringEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "XText", "显示文本")]
        public string XText
        {
            get
            {
                return _XText;
            }
            set
            {
                _XText = value;
                ApplyXProperty();
            }
        }

        public double DataAnimationProgress
        {
            get
            {
                return (double)GetValue(DataAnimationProgressProperty);
            }
            set
            {
                SetValue(DataAnimationProgressProperty, value);
            }
        }

        public static DependencyProperty DataAnimationProgressProperty = DependencyProperty.Register("DataAnimationProgress",
            typeof(double),
            typeof(DataTextAnimationLabel),
            new PropertyMetadata(default(double), OnDataAnimationProgressPropertyChangedCallback));

        private static void OnDataAnimationProgressPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataTextAnimationLabel dObject = d as DataTextAnimationLabel;
            dObject.UpdateDataAnimationProcess();
        }

        private void UpdateDataAnimationProcess()
        {
            if (!string.IsNullOrWhiteSpace(XText))
            {
                int progress = (int)this.DataAnimationProgress;
                if(progress == _labelDictonary.Count * _labelDictonary.Count)
                {
                    return;
                }
                int textIndex = progress / _labelDictonary.Count;
                int charIndex = (progress + 1) % _labelDictonary.Count;
                foreach(var item in _labelDictonary[textIndex].Children)
                {
                    (item as System.Windows.Controls.Label).Content = XText[(charIndex + textIndex) % _labelDictonary.Count];
                }
            }
        }
    }
}
